using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace Camalon.ColorPicker
{
	/// <summary>
	/// Summary description for ColorTraker.
	/// </summary>
    [ToolboxItem(false)]
    public class ColorTrackerOpacity : ColorTrackerBase //System.Windows.Forms.TrackBar
	{
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;


		public ColorTrackerOpacity():base()
		{
	
			InitializeComponent();


			// TODO: Add any initialization after the InitializeComponent call
			ColorRenderingValue.OpacityChanged+=new EventHandler(ColorRenderingValue_OpacityChanged);
			ColorRenderingValue.ValueChanged+=new EventHandler(ColorRenderingValue_ValueChanged);
			this.Minimum = 0;
			this.Maximum = 255;
			this.Value = ColorRenderingValue.Opacity;

			_dlg = new TransparentWindow();
			_dlg.Show();
			_dlg.Visible = false;


		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this)).EndInit();

		}
		#endregion

		private void ColorRenderingValue_OpacityChanged(object sender, EventArgs e)
		{
			if( _eventsLocked) return;
			if( this.Value != ColorRenderingValue.Opacity)
				base.Value = ColorRenderingValue.Opacity;
			this.Invalidate();
		}

		private void ColorRenderingValue_ValueChanged(object sender, EventArgs e)
		{
			if( _eventsLocked) return;
			if( this.Value != ColorRenderingValue.Opacity)
				this.Value = ColorRenderingValue.Opacity;
			this.Invalidate();
		}

		private bool _eventsLocked;
		protected override void ResetColorRenderingValue()
		{
			if( _eventsLocked) return;

			_eventsLocked = true;
			
			int value = (int)Math.Round( (double)this.Value );
			ColorRenderingValue.Opacity		= value;
			this.Invalidate();

			_eventsLocked = false;

		}


		TransparentWindow _dlg;

		protected override void OnMouseDown(MouseEventArgs e)
		{
			base.OnMouseDown (e);

			if( e.Button == MouseButtons.Left) 
			{
				
				_dlg.Location = PointToScreen( new Point( this.Right, e.Y));
				_dlg.Visible = true;
			}

		}


		protected override void OnMouseMove(MouseEventArgs e)
		{
			base.OnMouseMove (e);
			_dlg.Location = PointToScreen( new Point( this.Right, e.Y));
		}


		protected override void OnMouseUp(MouseEventArgs e)
		{
			base.OnMouseUp (e);
			
			if( e.Button == MouseButtons.Left) 
			{
				_dlg.Visible = false;
			}

		}



		#region DRAWING

		protected override  void UpdateSliderColor( Graphics graphics, Rectangle colorRegion)
		{
			if( graphics == null) throw new ArgumentNullException("graphics","Graphics object can not be null");

			Rectangle r = colorRegion;

			graphics.FillRectangle( Brushes.White, r);
			Rectangle r2 = r;
			r2.Width =(r2.Width/2)+1;
			graphics.FillRectangle( Brushes.Black, r2);

			Color color = ColorRenderingValue.Color;
			using ( LinearGradientBrush lgb = new LinearGradientBrush( r , color, Color.FromArgb( 0, color), 90f, false ) ) 
				graphics.FillRectangle(lgb, r); 


		}


		#endregion DRAWING



		private class TransparentWindow:System.Windows.Forms.Form
		{

			public TransparentWindow()
			{
				this.SetStyle( ControlStyles.DoubleBuffer, true);
				this.SetStyle( ControlStyles.UserPaint, true);
				this.SetStyle( ControlStyles.AllPaintingInWmPaint, true);


				ColorRenderingValue.OpacityChanged+=new EventHandler(ColorRenderingValue_OpacityChanged);
				ColorRenderingValue.ValueChanged+=new EventHandler(ColorRenderingValue_ValueChanged);
			
				this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
				this.ShowInTaskbar = false;
				this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
				this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
				this.BackColor = Color.White;
				this.MinimumSize = new Size( 10,10);
				this.Size= new System.Drawing.Size(60, 60);
			
			}

			private void ColorRenderingValue_OpacityChanged(object sender, EventArgs e)
			{
				if( this.Visible) this.Invalidate();
			}

			private void ColorRenderingValue_ValueChanged(object sender, EventArgs e)
			{
				if( this.Visible) this.Invalidate();
			}

			protected override void OnPaint(PaintEventArgs e)
			{
				Graphics g = e.Graphics;
				RectangleF clientRect = this.ClientRectangle;
				clientRect.Width /=2f;
				clientRect.Height /=2f;
				g.FillRectangle( Brushes.Black, clientRect );
				clientRect.X +=clientRect.Width;
				clientRect.Y +=clientRect.Height;
				g.FillRectangle( Brushes.Black, clientRect );

				clientRect = this.ClientRectangle;
				Color c = ColorRenderingValue.ARGBColor;
				using ( SolidBrush b = new SolidBrush( c ))
				g.FillRectangle( b,clientRect);

				using( Pen p = new Pen( Color.DarkGray, 2f))
				g.DrawRectangle( p,this.ClientRectangle);
			}


		}
	}
}
